#ifndef _INCLUDES_H_
#define _INCLUDES_H_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <time.h>

#include <pthread.h>
#include <ctype.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/stat.h>
#include "sys/ipc.h"
#include "sys/msg.h"
#include "sys/time.h"
#include <sys/ioctl.h>
#include <semaphore.h>


#define VERSION_DISPLAYER       1
#define NEW_HARDWARE_HW         1
#define LCD_TYPE_FSD            1

#define SET_OFF                 0
#define SET_ON                  1

//rtc
#define PIN_NUM_RTC_CLK         6
#define PIN_NUM_RTC_SDA         15

//rs485
#define PIN_NUM_RS485_TX        2
#define PIN_NUM_RS485_RX        1
#define PIN_NUM_RS485_EN        -1

#if NEW_HARDWARE_HW
    //beep
    #define PIN_NUM_BEEP            0

    //wifi
    #define PIN_NUM_WIFI_EN         36
    #define PIN_NUM_WIFI_RX         43
    #define PIN_NUM_WIFI_TX         44
#else
    //beep
    #define PIN_NUM_BEEP            36

    //wifi
    #define PIN_NUM_WIFI_EN         0
    #define PIN_NUM_WIFI_RX         43
    #define PIN_NUM_WIFI_TX         44
#endif

#define PIN_NUM_BCKL            4

#define app_printf printf
#define app_reboot esp_restart


typedef union 
{          
      struct        
      {
            unsigned b0:1;
            unsigned b1:1;
            unsigned b2:1;
            unsigned b3:1;
            unsigned b4:1;
            unsigned b5:1;
            unsigned b6:1;
            unsigned b7:1;
      }bit;
      unsigned char byte;
}bit_char;

typedef union 
{          
      struct        
      {
            unsigned b0:1;
            unsigned b1:1;
            unsigned b2:1;
            unsigned b3:1;
            unsigned b4:1;
            unsigned b5:1;
            unsigned b6:1;
            unsigned b7:1;
            unsigned b8:1;
            unsigned b9:1;
            unsigned b10:1;
            unsigned b11:1;
            unsigned b12:1;
            unsigned b13:1;
            unsigned b14:1;
            unsigned b15:1;
      }bit;
      unsigned int word;
}bit_word;

typedef union {
    struct	 
    {
    	unsigned b0:1;
    	unsigned b1:1;
    	unsigned b2:1;
    	unsigned b3:1;
    	unsigned b4:1;
    	unsigned b5:1;
    	unsigned b6:1;
    	unsigned b7:1;
    }bit;
    struct	 
    {
    	unsigned L4:4;
    	unsigned H4:4;
    }bit4;
	unsigned char byte;
}byte_def;


typedef union{
    struct	 
    {
	unsigned L4:4;
	unsigned H4:4;
    }bit4;
	unsigned char byte;
}byte_half_def;


typedef union {
	struct{
		unsigned char	low;	
		unsigned char	high;	
	} byte;
	unsigned short		word;
}word_def;


typedef union {
	struct{
		unsigned char	byte0;
		unsigned char	byte1;
		unsigned char	byte2;
		unsigned char	byte3;
	} byte;
	struct{
		unsigned short	word0;
		unsigned short	word1;
	} word;
	unsigned long int	_2word;
}uint32_def;

#define set_bits(w,x,y,z) ((w & (~(((1 << y) - 1) << x))) + (z << x))


typedef struct user_set_t{  
    uint8_t f_On;
    uint8_t Mode_Auto;
    uint8_t f_KeyLock;
    uint8_t f_Heater;
    uint8_t f_HeatTime;
    uint8_t f_LowPriceTime;
    uint8_t f_CoolProtectLevel;
    
    uint8_t f_DF;
    uint8_t f_FD;
    uint8_t f_Recycle;
    uint8_t f_Quick;
    uint8_t f_Disinfection;
    uint8_t f_Pump;
    uint8_t f_CoolProtect;
    uint8_t f_Keep;
    uint8_t f_Solar;
    uint8_t f_IO;
    uint8_t f_Quiet;
    uint8_t f_Intellect;
    uint8_t f_ECO;
    uint8_t f_Holiday;

    uint8_t ViewTemp;
    uint16_t SysID;
    uint16_t ErrorCode;

    uint8_t Mode;
    uint8_t ModeFrq;
    uint8_t Fan;
    uint8_t ScheNum;
    uint8_t PrjSetDev;
    uint8_t PrjSetFlag;
    uint16_t PrjSetAddr;
    uint16_t PrjSetValue;
    uint16_t PrjSetMax;
    uint16_t PrjSetMin;
    uint16_t PrjSetType;

    uint8_t Test_Touch1;
    uint8_t Test_Touch2;
    uint8_t Test_Touch3;
    uint8_t Test_Touch4;
    uint8_t Test_Touch5;
    uint8_t Test_TX;
    uint8_t Test_RX;

    uint8_t TimerMode;
    uint8_t online_test;
    uint8_t SchedNvs;
    uint8_t KeyPress;
    uint8_t f_DF_Timer;
    uint8_t Updata2CommCnt;
    uint8_t Updata2Network;
    uint8_t CleanErrCnt;
    uint8_t UpgradeTimer;
    uint8_t UpgradeTimeout;
    uint16_t UpgradeTotal;
    uint16_t UpgradeCur;

    int16_t SetTemp[10];
    int16_t SetTempMax[10];
    int16_t SetTempMin[10];

    int16_t MemoryOn;
    int16_t BeepEn;
    int16_t BlkMode;
    int16_t FaultTone;
    int16_t Lauguage;
    int16_t BlkTimer;
    uint8_t Blink;
    int16_t WifiSet;
    int16_t WifiEn;
    int16_t Password;
    int16_t unit;

    int16_t temp_outlet;
    int16_t temp_tank;
    int16_t temp_return;
    int16_t temp_env;
    int16_t blk_level;
    int16_t blk_time;
    int16_t blk_time_count;
    int16_t password;
    int16_t comp_frq;

    uint8_t f_hs;
    uint8_t f_ysj;
    uint8_t f_sb;
    uint8_t f_djr;
    uint8_t f_yc;
    uint8_t f_stf;
    uint8_t f_fj;
    uint8_t f_jy;

    int16_t status_menu_sel;
    int16_t status_cur_page;
    int16_t status_total_page;

    int16_t  setting_menu_index;
    int16_t  setting_menu_page;

    int16_t setting_menu_sel;
    int16_t setting_cur_page;
    int16_t setting_total_page;

    int16_t error_cur_page;
    int16_t error_total_page;

    int16_t history_cur_page;
    int16_t history_total_page;

    float Test_TEMP;
    float Test_HUM;
    uint32_t TimerPwrOn;
    unsigned long  firm_len_cur;
    unsigned long  firm_len_total;
}user_set_t;

enum
{
    USER_SET_MODE_AUTO,
    USER_SET_MODE_COOL,
    USER_SET_MODE_HEAT,
    USER_SET_MODE_WATER,
    USER_SET_MODE_WATER_COOL,
    USER_SET_MODE_WATER_HEAT,
    USER_SET_MODE_FAN,
    USER_SET_MODE_DRY,
    USER_SET_MODE_SPA,
    USER_SET_MODE_MAX,
};

extern user_set_t g_UserSet;

int sleep_ms(double sleep_time);
#endif